#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/9/4 19:04
# @Author  : 爱吃荷包蛋
# @USER    : Connor
# @File    : writeBullseye.py
# @Software: PyCharm
from matplotlib import pyplot as plt
import matplotlib.image as mping
import numpy as np


# >>>
# ima = mping.imread('E:/InVesalius/data/slice1.png')
# imageData = ima
# imageData = imageData[:,:,0]*255
# # ima = mping.imread('E:/InVesalius/matlab/AHABullseye.png')
# # ima-pixel ->(r,g,b)*255->r(value)
# im = ima[:,:,1]*255
# plt.imshow(im)
# plt.show()
# >>>
# parameter:
# N = 4# N :number of slices
# RN=256# result_image :256*256--rN*rN
# result_image = [[0]*RN]*RN
# # def writeBullseye(imageData,p,betap):
# imageData = np.mat(np.random.random((10,10))*100)

def Get_pixel_border(imageData):
    ##----------
    # imageData[:,:,0] = imageData[:,:,0]*255
    # imageData[:,:,1]=0
    # imageData = ima[:,:,0:2]
    ##-----------image value caculate
    (row, column) = imageData.shape  # hang lie (0-> piexl;1->angle)
    pixel_value = [[] for i in range(36)]  # default piexl value
    mean_value = [0 for i in range(36)]  # every 10 degree ->Init value ->> will return
    median_value = [0 for i in range(36)]  # default piexl value
    vector0 = np.array([1, 0])
    xorigin = (row - 1) / 2.0
    yorigin = (column - 1) / 2.0
    for y in range(column):
        for x in range(row):
            vector1 = np.array([x - xorigin, yorigin - y])
            cos_angle = vector0.dot(vector1) / np.sqrt(vector1.dot(vector1))
            angle = np.arccos(cos_angle) * 180 / np.pi
            if yorigin - y < 0:
                angle = 360 - angle
            pos = int(angle / 10)
            # piexl_value[pos] = imageData[x,y]
            if imageData[x, y] > 100:
                pixel_value[pos].append(imageData[x, y])
    # print 'max value:',max_value
    # medan_result = [0 for i in range(36)]
    for i in range(36):
        pixel_list = pixel_value[i]
        pixel_list.sort()
        plen = len(pixel_list)
        median_value[i] = (pixel_list[plen / 2] + pixel_list[~(plen / 2)]) / 2  # code optimization
        mean_value[i] = sum(pixel_list) / plen
        # if plen%2==0:  #numbei is even, esle: odd
        #     median_value[i] = (pixel_list[plen/2-1]+pixel_list[plen/2])/2
        # else:
        #     median_value[i] = pixel_list[(plen-1)/2]
    print('median value:', median_value)
    # print 'mean value:',mean_value

    return median_value
    # return mean_value


# #-------------------------------
# N = 4  # N :number of slices
# sli_ID = 1 # image slice position ID
# # #image slice ID 1,2,3,4....,N
def creatBull(pixel, N, sli_ID):
    RN = 256  # result_image :256*256--rN*rN
    rad_list = [i * RN * 0.5 / N for i in range(N + 1)]
    result_image = np.array([[0] * RN] * RN)
    xre = (RN - 1) / 2.0
    yre = (RN - 1) / 2.0
    vector0 = np.array([1, 0])
    for y in range(RN):
        for x in range(RN):
            vector1 = np.array([x - xre, yre - y])
            dis = np.sqrt(vector1.dot(vector1))
            if rad_list[sli_ID] > dis > rad_list[sli_ID - 1]:
                cos_angle = vector0.dot(vector1) / dis
                angle = np.arccos(cos_angle) * 180 / np.pi
                if yre - y < 0:
                    angle = 360 - angle
                pos = int(angle / 10)
                result_image[x, y] = pixel[pos]
    return result_image


# ima = mping.imread('E:/InVesalius/data/slice1.png')
# imageData = ima
# imageData = imageData[:,:,0]*255
# max_pixel = Get_pixel_border(imageData)
# N = 4  # N :number of slices
# sli_ID = 1 # image slice position ID
# # #image slice ID 1,2,3,4....,N
# reim1 = creatBull(max_pixel,N,sli_ID)
ima1 = mping.imread('E:/InVesalius/data/slice1.png')
ima2 = mping.imread('E:/InVesalius/data/slice2.png')
ima3 = mping.imread('E:/InVesalius/data/slice3.png')
ima4 = mping.imread('E:/InVesalius/data/slice4.png')
imageData1 = ima1[:, :, 0] * 255
imageData2 = ima2[:, :, 0] * 255
imageData3 = ima3[:, :, 0] * 255
imageData4 = ima4[:, :, 0] * 255
max_pixel1 = Get_pixel_border(imageData1)
max_pixel2 = Get_pixel_border(imageData2)
max_pixel3 = Get_pixel_border(imageData3)
max_pixel4 = Get_pixel_border(imageData4)
N = 4  # N :number of slices
# sli_ID = 1 # image slice position ID
# #image slice ID 1,2,3,4....,N
reim = creatBull(max_pixel1, N, 1)
reim = creatBull(max_pixel2, N, 2) + reim
reim = creatBull(max_pixel3, N, 3) + reim
reim = creatBull(max_pixel4, N, 4) + reim
plt.imshow(reim)
plt.show()
